package com.originskyseed.platform.job.jobhandler.count;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.job.biz.model.ReturnT;
import com.originskyseed.platform.core.job.handler.IJobHandler;
import com.originskyseed.platform.core.job.handler.annotation.JobHandler;
import com.originskyseed.platform.core.job.log.JobLogger;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.model.PlayerRelateRecharge;
import com.originskyseed.platform.user.service.IPlayerRelateRechargeService;
import com.originskyseed.platform.user.service.IPlayerService;

/**
 * 统计玩家总充金额功能
 * 
 * @Author: twh @date: 2021/6/3
 */

@Component
@JobHandler(value = "playerRechargeTotalHandler")
public class playerRechargeTotalHandler extends IJobHandler {
	@DubboReference
	private IPlayerService playerService;

	@DubboReference
	private IPlayerRelateRechargeService playerRelateRechargeService;

	@Override
	public ReturnT<String> execute(String tenantCode) throws Exception {
		try {
			if (StringUtils.isNotEmpty(tenantCode)) {
				TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
			}
			//查询新增的玩家信息
			int pageSize=1;
			int pageNo=200;
			HashMap params = new HashMap<>();
			/*params.put("pageSize", pageSize);
			params.put("pageNo", pageNo);
			BasePage<Player> page = new BasePage<Player>(pageNo, pageSize);
			page = this.playerService.selectPlayerListByPageByTenantCode(tenantCode,params,page);
			if (page != null && page.getTotalPage() != null && page.getTotalPage() > 0) {
				List<Player> results = page.getResults();
				if (results != null && results.size() > 0) {
					playerService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
				}
				int count = page.getTotalPage();

				if (count > 1) {
					for (int i = 2; i <= count; i++) {
						pageNo = i;
						params.put("pageNo", pageNo);
						page = new BasePage<Player>(pageNo, pageSize);
						page = this.playerService.selectPlayerListByPageByTenantCode(tenantCode,params,page);
						results = page.getResults();
						if (results != null && results.size() > 0) {
							playerService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
						}
					}
				}
			}*/

			//查询历史玩家新的充值信息
			params.put("pageSize", pageSize);
			params.put("pageNo", pageNo);
			params.put("recharge_is_update",0);
			BasePage<PlayerRelateRecharge> rechargePage = new BasePage<PlayerRelateRecharge>(pageNo, pageSize);
			rechargePage = this.playerRelateRechargeService.selectListByPageByTenantCode(tenantCode,params,rechargePage);
			if (rechargePage != null && rechargePage.getTotalPage() != null && rechargePage.getTotalPage() > 0) {
				List<PlayerRelateRecharge> results = rechargePage.getResults();
				if (results != null && results.size() > 0) {
					playerService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
				}
				int count = rechargePage.getTotalPage();

				if (count > 1) {
					for (int i = 2; i <= count; i++) {
						pageNo = i;
						params.put("pageNo", pageNo);
						rechargePage = new BasePage<PlayerRelateRecharge>(pageNo, pageSize);
						rechargePage = this.playerRelateRechargeService.selectListByPageByTenantCode(tenantCode,params,rechargePage);
						results = rechargePage.getResults();
						if (results != null && results.size() > 0) {
							playerService.updateByPrimaryKeySelectiveByTenantCode(tenantCode,results);
						}
					}
				}
			}
		} catch (Exception e) {
			JobLogger.log("统计玩家总充金额数据：" + getExceptionDetail(e));
			return FAIL;
		}
		return SUCCESS;
	}


	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 *
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Exception ex) {
		String ret = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PrintStream pout = new PrintStream(out);
		try {
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
		} catch (Exception e) {
		} finally {
			try {
				pout.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
}
